package com.woniuxy.exception;

import com.woniuxy.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: linKe
 * @Date: 2021/11/25 8:11
 * @FileName: GlobalExceptionHandler
 * @version: 1.0
 * @Description: 描述
 */
@Log4j2
@ControllerAdvice(basePackages = "com.woniuxy.controller")
public class GlobalExceptionHandler {


    /**
     * 统一异常处理@ExceptionHandler,主要用于CustomException   运行时异常
     *
     * @param request 请求对象
     * @param e       异常对象
     * @return 返回json串
     */
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public Result<Throwable> customer(HttpServletRequest request, CustomException e) {
        log.info("异常信息：", e);
        return new Result<>(e.getCode(), e.getMsg(),e.getCause());
    }
    /**
     * 统一异常处理@ExceptionHandler,主要用于Exception
     *
     * @param request 请求对象
     * @param e       异常对象
     * @return //返回json串
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<Throwable> error(HttpServletRequest request, Exception e) {
        log.error("异常信息：", e);
        return new Result<>("500","系统异常",e.getCause());
    }


}
